<template>
  <!--批量活动执行情况查询厂端-->
  <div>
    <TopFilter ref="topfilter" :list="topFilterList" :cols="3" labelWidth="100" :collapse="true" @filterChange="changeHandle" @resetChange="resetHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      :isExportExcel="false"
      exportFileName="批量活动执行情况查询.xlsx"
      :onCalcExportData="handleExportFun"
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      datakey="records"
      :isSelectColumn="false"
      :onColumnsChange="(columns) => (this.columns = columns)"
    >
      <template slot="controls">
        <!-- <multiuse-button type="primary" :click="exportHandle.bind(this)">导出</multiuse-button> -->
      </template>
    </FilterTable>

    <BaseDialog :visible.sync="selectDelearFlag" title="服务中心选择" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <DealerList @close="Delearclose" @save="Delearsave" selectionType="multiple" />
    </BaseDialog>

     <Drawer :visible.sync="visibleAudit" :title="title" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <audit :row="row" :type="type" @close="closeHandler" />
    </Drawer>

  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import audit from './audit';
import { arrayObjDeWeight } from '@/utils';
import { dealerMixin } from '@/pages/repair/batchEventManagement/dealerMixin';
import DealerList from '@/pages/claimDemage/tools/DealerList.vue';
import { getScrapInfo} from '@/api/repair/usedScrap/usedScrapAudit';

export default {
  name: 'scrapIndex',
  mixins: [dictionary, dealerMixin],
  data() {
    this.BaseTable = null;
    this.conf = { add: '新增', edit: '编辑', show: '审核' };
    return {
      topFilterList: this.createTopFilterList(),
      visible: false,
      fetchApi: getScrapInfo,
      dialogTitle: '',
      title:'',
      fetchParams: { xhrAbort: true,state:55711002 },
      showDetail: false,
      visibleDetail: false,
      visibleAudit:false,
      rowData: {},
      formData: {},
      selectDealerData: [],
      dealerData: [],
      selectDelearFlag: false,
      columns: this.createTableColumns(),
      handleType: '',
      type: 1,
      resDealer: '',
      /*执行经销商*/
      ownerCode: '',
      nrId: '',
    };
  },
  components: {
    DealerList,audit
  },
 async created(){
  },
  mounted() {
    // this.getAllDelears(this.columns, ['ownerCode'], 'table');

    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {

    selectFun(list) {
      let row = list[0];
      if (this.type == 1) {
        //经销商
        // this.resDealer = row['dealerCode'];
        // this.topFilterList.find(item => item.fieldName == 'resDealerName').initialValue = row['dealerName'];
      } else {
        //执行经销商
        this.ownerCode = row['dealerCode'];
        this.topFilterList.find((item) => item.fieldName == 'ownerName').initialValue = row['dealerName'];
      }
    },

    // 导出处理函数
    handleExportFun(row) {
      row.roStatus = this.createDictText(row.roStatus, 8049);
    },

    /*1 经销商查询 2 执行经销商*/
    seachFun(type) {
      this.type = type;
      this.visible = true;
    },

    closeDialog() {
      this.visible = false;
    },

    async initSelect() {
      let res = await getAllUser();
      // 去重
      let dataList = arrayObjDeWeight(res.data, 'userId');

      this.columns.find((item) => item.dataIndex === 'maBy').dictItems = dataList.map((x) => ({
        text: x.employeeName,
        value: x.userId,
      }));
    },

    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 85,
          render: (props) => {
            return (
              <div>
                <el-button size="small" type="text" disabled ={props.row.state + '' === '55711003' || props.row.state + '' === '55711004' } onClick={() => this.auditHandle(props.row,'edit')}>
                  审核
                </el-button>
                <el-button size="small" type="text" onClick={() => this.auditHandle(props.row,'detail')}>
                  明细
                </el-button>

              </div>

            );
          },
        },

        {
          title: '报废单号',
          dataIndex: 'scrapNo',
          width: 150,
          sorter: true,
        },

        {
          title: '服务中心代码',
          dataIndex: 'dealerCode',
          sorter: true,
        },
        {
          title: '服务中心简称',
          width: 180,
          dataIndex: 'dealerName',
          sorter: true,
        },
        // {
        //   title: '回运仓库',
        //   dataIndex: 'whName',
        //   sorter: true,
        // },
        {
          title: '状态',
          dataIndex: 'state',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(5571),
          render: (props) => {
            return <span>{this.createDictText(props.row.state, 5571)}</span>;
          },
        },
        {
          title: '报废类型',
          dataIndex: 'scrapType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(5570),
          render: (props) => {
            return <span>{this.createDictText(props.row.scrapType, 5570)}</span>;
          },
        },
        {
          title: '旧件代码',
          dataIndex: 'partCode',
          sorter: true,
        },
        {
          title: '旧件名称',
           width: 160,
          dataIndex: 'partName',
          sorter: true,
        },
        {
          title: '报废数量',
          dataIndex: 'scrapNum',
          sorter: true,
        },
        {
          title: '上报人',
          dataIndex: 'escalationBy',
          sorter: true,
        },
        {
          title: '上报日期',
          dataIndex: 'escalationData',
          width: 150,
          sorter: true,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          filter: true,
          filterType: 'date-range',
        },
        {
          title: '审核人',
          dataIndex: 'checkBy',
          sorter: true,
        },
        {
          title: '审核日期',
          dataIndex: 'checkData',
          editType: 'date-picker',
          width: 150,
          sorter: true,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          filter: true,
          filterType: 'date-range',
        },
        {
          title: '报废原因',
          dataIndex: 'complainReason',
          sorter: true,
          width: 150,
          sorter: true,
        },
      ];
    },
    createTopFilterList() {
      return [
        // 报废单号、服务中心、上报日期、单据状态、报废类型、旧件代码、旧件名称；
        {
          type: 'INPUT',
          label: ' 报废单号',
          fieldName: 'scrapNo',
          placeholder: ' 报废单号...',
        },
        {
          type: 'INPUT',
          label: '服务中心',
          render: (p) => {
            return (
              <div>
                <el-select v-model={this.selectDealerData} disabled={true} multiple placeholder="请选择">
                  {this.dealerData.map((item) => (
                    <el-option key={item.dealerCode} label={item.dealerShortname} value={item.dealerCompanyCode} />
                  ))}
                </el-select>
                <multiuse-button style="position: absolute;right: 0;height:100%" icon="el-icon-search" onClick={() => (this.selectDelearFlag = true)} />
              </div>
            );
          },
        },
        { type: 'RANGE_DATE', label: '上报日期', fieldName: 'escalationDataFrom|escalationDataTo', placeholder: '请选择', initialValue: '' },
        {
          type: 'SELECT',
          label: '单据状态',
          fieldName: 'state',
          placeholder: '请选择',
          initialValue:55711002,
          itemList: this.createDictList(5571,[55711001]),
       },

        {
          type: 'INPUT',
          label: '旧件代码',
          fieldName: 'partCode',
          placeholder: '旧件代码',

        },
        {
          type: 'INPUT',
          label: '旧件名称',
          fieldName: 'partName',
          placeholder: '旧件名称',

        },
        {
          type: 'SELECT',
          label: '报废类型',
          fieldName: 'scrapType',
          placeholder: '请选择',
          initialValue:'',
          itemList: this.createDictList(5570),
        },
      ];
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({},val,{ dealerCode: this.searchDealerCode, dealerCompanyCode: this.selectDealerData.length ? this.selectDealerData : null });

    },
    detailHandle(row) {
      this.nrId = row['nrId'];
      this.formData = row;
      this.handleType = 'show';
      this.showHandler();
    },
    auditHandle(row,type) {
      this.title = type == 'edit'?'旧件报废审核':'旧件报废明细';
      this.type = type;
      this.row = row;
      this.visibleAudit = true;
    },

    showHandler() {
      this.visibleDetail = true;
    },
       Delearclose() {
      this.selectDelearFlag = false;
    },
    Delearsave(obj) {
      console.log('选择数据传递', obj);
      this.dealerData = obj;
      this.selectDealerData = [];
      obj.forEach(item => {
        this.selectDealerData.push(item.dealerCompanyCode);
      });
      this.selectDelearFlag = false;
    },
    closeHandler(type) {
      this.visibleDetail = false;
      this.visibleAudit = false;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    resetHandle(val) {
      this.dealerData = [];
      this.selectDealerData = [];
      this.fetchParams = Object.assign({}, this.fetchParams, val, { dealerCompanyCode: null },  { xhrAbort: false,state:'' });
      this.$nextTick(() => {
              this.topFilterList.find(x => x.fieldName == 'state').initialValue = '';

      });
    },
  },
};
</script>

<style lang="less" scoped></style>
